package yiran.stock;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import yiran.stock.gui.StockGUI;
import yiran.stock.lang.LangHelper;
import yiran.stock.command.*;

import java.util.*;

public class StockCommand implements CommandExecutor, TabCompleter, Listener {
    private final Stock plugin;
    private final Map<String, StockSubCommand> subCommands = new HashMap<>();
    public static Map<UUID, StockGUI> openGUIs = new HashMap<>();

    public StockCommand(Stock plugin) {
        this.plugin = plugin;

        subCommands.put("view", new ViewCommand(plugin));
        subCommands.put("buy", new BuyCommand());
        subCommands.put("holdings", new HoldingsCommand());
        subCommands.put("sell", new SellCommand());
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (args.length == 0) {
            sendHelp(sender);
            return true;
        }

        StockManager stockManager = plugin.getStockManager();
        if (stockManager == null) {
            sender.sendMessage(LangHelper.getString("errors.can-not-use"));
            return true;
        }

        String subCommand = args[0].toLowerCase();
        StockSubCommand handler = subCommands.get(subCommand);

        if (handler != null) {
            String[] subArgs = Arrays.copyOfRange(args, 1, args.length);
            return handler.execute(sender, subArgs, plugin, stockManager);
        } else {
            sendHelp(sender);
            return true;
        }
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command cmd, String alias, String[] args) {
        List<String> completions = new ArrayList<>();
        StockManager stockManager = plugin.getStockManager();

        if (args.length == 1) {
            completions.addAll(subCommands.keySet());
        } else if (args.length >= 2) {
            String subCommand = args[0].toLowerCase();
            StockSubCommand handler = subCommands.get(subCommand);
            if (handler != null) {
                String[] subArgs = Arrays.copyOfRange(args, 1, args.length);
                return handler.tabComplete(sender, subArgs, stockManager);
            }
        }
        return completions;
    }

    private void sendHelp(CommandSender sender) {
        LangHelper.getStringList("commands.help").forEach(sender::sendMessage);
    }

    public static List<String> getStockSymbolCompletions(String[] args, StockManager stockManager) {
        List<String> completions = new ArrayList<>();
        if (stockManager == null) return completions;

        String input = args.length > 0 ? args[0].toLowerCase() : "";
        stockManager.getStockPrices().keySet().forEach(symbol -> {
            if (symbol.toLowerCase().startsWith(input)) {
                completions.add(symbol.toLowerCase());
            }
        });
        return completions;
    }

    @EventHandler
    public void onInventoryClose(InventoryCloseEvent event) {
        String title = event.getView().getTitle();
        if (title.startsWith(LangHelper.getString("gui.title")) ||
                title.startsWith(LangHelper.getString("gui.buy-title-prefix"))) {

            if (event.getPlayer() instanceof Player) {
                UUID uuid = event.getPlayer().getUniqueId();
                openGUIs.remove(uuid);
                plugin.getBuyManager().clearSelection((Player) event.getPlayer());
            }
        }
    }

}
